package com.master.common.utils;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.security.MessageDigest;

/**
 * @author Heath.Que
 * @version 1.0
 * @classname
 * @description MD5加密工具类
 * @Date 2021/3/24 14:30
 **/

public class EncryptUtil {
    private static final Logger logger = LoggerFactory.getLogger(EncryptUtil.class);

    private static final String mark = "MD5";//加密类型:MD5

    public EncryptUtil() {
    }

    public static String encrypt (String encryptingObj){
        return getDigest(encryptingObj);
    }

    private static String getDigest(String encryptingObj){
        try {
            MessageDigest mark = MessageDigest.getInstance(EncryptUtil.mark);
            mark.update(encryptingObj.getBytes());
            byte[] digest = mark.digest();
            return byte2hex(digest);
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    private static String byte2hex(byte[] a){
        StringBuilder buffer = new StringBuilder();
        String obj = "";

        for (byte b : a){
            obj = Integer.toHexString(b & 255);
            if (obj.length() == 1){
                buffer.append("0");
            }
            buffer.append(obj);
        }
        return buffer.toString();
    }


    public static void main(String[] args) {
        System.out.println(encrypt("123456"));
    }
}
